<template>
    <el-card style="width: 30%;height: 70%;position: fixed;top: 10%;left: 35%;">
        <div slot="header" class="clearfix">
            <span>新增角色</span>
        </div>
        <div>
            <el-form ref="form" :model="form" label-width="80px">

                <el-form-item label="角色名">
                    <el-input v-model="form.roleName"></el-input>
                </el-form-item>

                <el-form-item label="描述信息">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>


                <el-form-item label="角色权限">
                    <el-select v-model="selectPermissionIds" multiple placeholder="请选择">
                        <el-option v-for="item in query_permissions" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>


                <el-form-item>
                    <el-col :span="12">
                        <el-button type="primary" @click="onSubmit">立即创建</el-button>
                    </el-col>
                    <el-col :span="12">
                        <router-link to="/home/user">
                            <el-button> 取消 </el-button>
                        </router-link>
                    </el-col>
                </el-form-item>
            </el-form>
        </div>
    </el-card>
</template>

<script>

import { getPermissionList } from '@/api/permission.js'
import { addRole } from '@/api/role.js'

export default {
    data() {
        return {
            form: {
                roleName: "",
                description: "",
                permissions: []
            },
            query_permissions: [],
            selectPermissionIds: []
        }
    },
    methods: {
        onSubmit() {
            for (let i = 0; i < this.selectPermissionIds.length; i++) {
                let obj = { permissionId: this.selectPermissionIds[i] }
                this.form.permissions.push(obj)
            }

            addRole(this.form).then((result) => {

                this.$message.success('新增成功')
                this.$router.push('/home/role')

            }).catch((err) => {

            });
        }
    },
    mounted() {
        getPermissionList().then((result) => {
            this.query_permissions = result.data.map(function (obj) {
                return { value: obj.permissionId, label: obj.permissionName };
            });
        }).catch((err) => {

        });
    }
}
</script>

<style>